package pw.orm.db;

import pw.orm.core.DefLog;
import pw.orm.core.ILog;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @Author Eric
 * @Desc
 * @Date Create in 17-12-29 下午5:05
 * @Modified By
 */
public class JormConfig {
    private boolean isDebug = false;
    private DataSource dataSource ;
    private String configName = "main"; // 默认的配置名称
    private ILog log = new DefLog();

    public DataSource getDataSource() {
        return dataSource;
    }

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public String getConfigName() {
        return configName;
    }

    public void setConfigName(String configName) {
        this.configName = configName;
    }

    public void isDebug(boolean isDebug){
        this.isDebug = isDebug;
    }
    public boolean isDebug(){
        return isDebug;
    }

    public static JormConfig create(String driver,String url,String user,String password){
        JormConfig config = new JormConfig();
        config.dataSource = new DefaultDataSource(driver,url,user,password);

        return config;
    }

    public Connection getConnection() throws SQLException{
        if (dataSource != null)
        return dataSource.getConnection();
        return null;
    }

    public void close(Connection connection) throws SQLException{
        if(connection != null)
        connection.close();
    }

    public ILog getLog() {
        return log;
    }

    public void setLog(ILog log) {
        this.log = log;
    }
}
